package com.ipemon.model;

import java.util.HashMap;
import java.util.Map;

public class Dresseur {

	private String identifiant;
	private Map<Integer,IPemon> ipemons = new HashMap<Integer,IPemon>();
	private IPemon defaultIPemon = null;
	
	public Dresseur(String identifiant) {
		this.identifiant = identifiant;
	}
	
	public String getIdentifiant() {
		return identifiant;
	}
	
	public void setIdentifiant(String identifiant) {
		this.identifiant = identifiant;
	}
	
	
	public void addIPemon(IPemon ipemon) {
		this.ipemons.put(ipemons.size(), ipemon);
	}
	
	public IPemon getDefaultIPemon() {
		if(defaultIPemon == null)
			defaultIPemon = ipemons.get(0);
		
		return defaultIPemon;
	}
	
	public void setDefaultIPemon(int index) {
		this.defaultIPemon = ipemons.get(index-1);
	}
	
	public Map<Integer, IPemon> getIPemonList() {
		return ipemons;
	}
	
	public static void displayIPemon(IPemon ipemon) {
		
		System.out.println("IPemon:\t\t" + ipemon.getIdentifiant() + " [" + ipemon.getEspece() + "]");
		System.out.println("Vie: \t\t" + ipemon.getVieCur() + "/" + ipemon.getVieMax());
		System.out.println("Rapidity: \t" + ipemon.getRapidite());
		System.out.println("Experience: \t" + ipemon.getExperience());

		System.out.println("\t\t" + "Feu\t" + "Eau\t" + "Elect\t" + "Plante\t" + "Air\t" + "Pierre");
		System.out.println("Attaque\t\t" + ipemon.getAttaque().getFeu() + "\t" + ipemon.getAttaque().getEau() + "\t" +
							ipemon.getAttaque().getElectric() + "\t" + ipemon.getAttaque().getPlante() + "\t" + 
							ipemon.getAttaque().getAir() + "\t" + ipemon.getAttaque().getPierre() + "\t");
		System.out.println("Defense\t\t" + ipemon.getDefense().getFeu() + "\t" + ipemon.getDefense().getEau() + "\t" +
				ipemon.getDefense().getElectric() + "\t" + ipemon.getDefense().getPlante() + "\t" + 
				ipemon.getDefense().getAir() + "\t" + ipemon.getDefense().getPierre() + "\t");
		
	}
	
	private static int ipemonPowerAttaque(int power1, int rap1, int exp1, int power2, int rap2, int exp2) {
		
		exp1 = (exp1 == 0) ? 1 : exp1;
		exp2 = (exp2 == 0) ? 1 : exp2;
		rap1 = (rap1 == 0) ? 1 : rap1;
		rap2 = (rap2 == 0) ? 1 : rap2;
		
		int powerattack = exp1 * rap1 * power1;
		int powerdefence = exp2 * rap2 * power2;
		
		int damage = powerattack - powerdefence;
		
/*		if(damage > 0) {
			damage = damage / (rap2 * exp2);
		} else {
			damage = damage / (rap1 * exp1);
		}*/
		
		return damage;
	}
	
	// returns true if ipemon1 wins
	public static boolean internalIPemonBattle(IPemon ipemon1, IPemon ipemon2) {
	
		boolean finished = false;
		
		IPemon attaquer  = ipemon1;
		IPemon defenser = ipemon2;
		
		Logger logger = Logger.getInstance();
		
		logger.log("Fight begins");
		
		int session = 0;
		while(!finished) {
			logger.log("<< Session " + ++session + " >>");
			logger.log("Attaquer is: " + attaquer.getIdentity() + "\t\t" + attaquer.getVieCur());
			logger.log("Defense is: " + defenser.getIdentity() + "\t\t" + defenser.getVieCur());
			
			int expA = attaquer.getExperience();
			int rapA = attaquer.getExperience();
			
			int expB = defenser.getExperience();
			int rapB = defenser.getRapidite();
			
			int defenserLoss = 0;
			int attacquerLoss = 0;
			
			//Fire attack 
			
			int firedamage = ipemonPowerAttaque(attaquer.getAttaque().getFeu(), rapA, expA, 
											defenser.getDefense().getFeu(), rapB, expB);
			if (firedamage > 0 ) 
				defenserLoss += Math.abs(firedamage);
			else 
				attacquerLoss += Math.abs(firedamage);
			
			//Eau damage
			
			int eaudamage = ipemonPowerAttaque(attaquer.getAttaque().getEau(), rapA, expA, 
					defenser.getDefense().getEau(), rapB, expB);
			if (eaudamage > 0 ) 
				defenserLoss += Math.abs(eaudamage);
			else 
				attacquerLoss += Math.abs(eaudamage);
			
			attaquer.setVieCur(attaquer.getVieCur() - attacquerLoss);
			defenser.setVieCur(defenser.getVieCur() - defenserLoss);

			
			logger.log("Attaquer loses: " + attacquerLoss);
			logger.log("Defenser loses: " + defenserLoss);
			
			if(attaquer.getVieCur() <= 0 || defenser.getVieCur() <= 0 )
				finished = true;
			
			IPemon temp = attaquer;
			attaquer = defenser;
			defenser = temp;
			
			if(session > 100)
				finished = true;
			
		}
		
		return (ipemon1.getVieCur() <= ipemon2.getVieCur() );
	}
	
	
	public static void main(String[] args) {
		Foret foret = new Foret();
		
		IPemon ipemon1 = IPemon.getIPemonByDataStream("2:8:61:91:2:5:(10:3:4:4:5:9):(3:10:4:4:9:5)");
		IPemon ipemon2 = IPemon.getIPemonByDataStream("3:5:55:80:3:4:(4:9:5:10:1:7):(9:4:10:5:7:1)");
		
		Dresseur.displayIPemon(ipemon1);
		System.out.println("");
		Dresseur.displayIPemon(ipemon2);
		System.out.println("");
		
		if(Dresseur.internalIPemonBattle(ipemon1, ipemon2))
			System.out.println("ipemon 1 wins");
		else 
			System.out.println("ipemon 2 wins");
		
		Logger.getInstance().printLogs();
		
	}
	
}
